1   /*
2    * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  package com.sun.media.sound;
26  
27  /**
28   * Lanczos interpolation resampler.
29   *
30   * @author Karl Helgason
31   */
32  public class SoftLanczosResampler extends SoftAbstractResampler {
33  
34      float[][] sinc_table;
35      int sinc_table_fsize = 2000;
36      int sinc_table_size = 5;
37      int sinc_table_center = sinc_table_size / 2;
38  
39      public SoftLanczosResampler() {
40          super();
41          sinc_table = new float[sinc_table_fsize][];
42          for (int i = 0; i < sinc_table_fsize; i++) {
43              sinc_table[i] = sincTable(sinc_table_size, -i
44                              / ((float) sinc_table_fsize));
45          }
46      }
47  
48      // Normalized sinc function
49      public static double sinc(double x) {
50          return (x == 0.0) ? 1.0 : Math.sin(Math.PI * x) / (Math.PI * x);
51      }
52  
53      // Generate sinc table
54      public static float[] sincTable(int size, float offset) {
55          int center = size / 2;
56          float[] w = new float[size];
57          for (int k = 0; k < size; k++) {
58              float x = (-center + k + offset);
59              if (x < -2 || x > 2)
60                  w[k] = 0;
61              else if (x == 0)
62                  w[k] = 1;
63              else {
64                  w[k] = (float)(2.0 * Math.sin(Math.PI * x)
65                                  * Math.sin(Math.PI * x / 2.0)
66                                  / ((Math.PI * x) * (Math.PI * x)));
67              }
68          }
69          return w;
70      }
71  
72      public int getPadding() // must be at least half of sinc_table_size
73      {
74          return sinc_table_size / 2 + 2;
75      }
76  
77      public void interpolate(float[] in, float[] in_offset, float in_end,
78              float[] startpitch, float pitchstep, float[] out, int[] out_offset,
79              int out_end) {
80          float pitch = startpitch[0];
81          float ix = in_offset[0];
82          int ox = out_offset[0];
83          float ix_end = in_end;
84          int ox_end = out_end;
85  
86          if (pitchstep == 0) {
87              while (ix < ix_end && ox < ox_end) {
88                  int iix = (int) ix;
89                  float[] sinc_table
90                          = this.sinc_table[(int) ((ix - iix) * sinc_table_fsize)];
91                  int xx = iix - sinc_table_center;
92                  float y = 0;
93                  for (int i = 0; i < sinc_table_size; i++, xx++)
94                      y += in[xx] * sinc_table[i];
95                  out[ox++] = y;
96                  ix += pitch;
97              }
98          } else {
99              while (ix < ix_end && ox < ox_end) {
100                 int iix = (int) ix;
101                 float[] sinc_table
102                         = this.sinc_table[(int) ((ix - iix) * sinc_table_fsize)];
103                 int xx = iix - sinc_table_center;
104                 float y = 0;
105                 for (int i = 0; i < sinc_table_size; i++, xx++)
106                     y += in[xx] * sinc_table[i];
107                 out[ox++] = y;
108 
109                 ix += pitch;
110                 pitch += pitchstep;
111             }
112         }
113         in_offset[0] = ix;
114         out_offset[0] = ox;
115         startpitch[0] = pitch;
116 
117     }
118 }